<template>
  <el-dialog
    :visible.sync="visible"
    :title="!dataForm.id ? $t('add') : $t('update')"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmitHandle()"
      label-width="80px"
    >
      <el-form-item prop="industryName" label="行业">
        <el-select v-model="dataForm.industryName" placeholder="请选择行业名称">
          <el-option
            v-for="item in industryNameList"
            :key="item"
            :value="item"
            :label="item"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="cloudMapName" label="云图名称">
        <el-input
          v-model="dataForm.cloudMapName"
          placeholder="请输入云图名称"
        ></el-input>
      </el-form-item>
      <el-form-item prop="sort" label="排序">
        <el-input-number
          v-model="dataForm.sort"
          controls-position="right"
          :min="0"
          label="排序"
        ></el-input-number>
      </el-form-item>

      <el-form-item prop="url" label="链接">
        <el-input
          type="textarea"
          v-model="dataForm.url"
          placeholder="请输入链接"
        ></el-input>
      </el-form-item>

      <el-form-item prop="iconUrl" label="图标">
        <el-upload
          name="file"
          :action="url"
          :before-upload="beforeUpload"
          :on-success="successHandle"
          :file-list="fileList"
          :limit="1"
          drag
          class="text-center"
          accept=".jpg, .jpeg, .png"
        >
          <i class="el-icon-upload"></i>
          <div class="el-upload__text" v-html="$t('upload.text')"></div>
          <div slot="tip" class="el-upload__tip" style="color: red;">只能上传jpg/png文件</div>
        </el-upload>
      </el-form-item>

      <el-form-item prop="status" label="状态">
        <el-radio-group v-model="dataForm.status">
          <el-radio :label="0">正常</el-radio>
          <el-radio :label="1">禁用</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t("cancel") }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{
        $t("confirm")
      }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
import Cookies from 'js-cookie'

import debounce from 'lodash/debounce'
export default {
  props: {
    industryNameList: Array
  },
  data () {
    return {
      visible: false,
      fileList: [],
      dataForm: {
        cloudMapName: '',
        createTime: '',
        creator: '',
        creatorName: '',
        iconUrl: '',
        iconUrlNew: '',
        id: '',
        industryName: '',
        isDel: 0,
        status: 0,
        menuType: '行业云图',
        sort: 0,
        updateTime: '',
        url: ''
      }
    }
  },
  computed: {
    url () {
      return `${
        window.SITE_CONFIG.apiURL
      }/sys/oss/api/profilePhotoUpload?token=${Cookies.get('token')}`
    },
    dataRule () {
      return {
        cloudMapName: [
          {
            required: true,
            message: this.$t('validate.required'),
            trigger: 'blur'
          }
        ],
        industryName: [
          {
            required: true,
            message: this.$t('validate.required'),
            trigger: 'blur'
          }
        ],
        sort: [
          {
            required: true,
            message: this.$t('validate.required'),
            trigger: 'blur'
          }
        ],
        url: [
          {
            required: true,
            message: this.$t('validate.required'),
            trigger: 'blur'
          }
        ],
        isDel: [
          {
            required: true,
            message: this.$t('validate.required'),
            trigger: 'blur'
          }
        ]
      }
    }
  },
  mounted () {},
  methods: {
    beforeUpload(file) {
      console.log('file.type', file.type)
      const isJPG = file.type === 'image/jpeg';
      if (!isJPG) {
        this.$message.error('上传图片只能是 JPG 或 PNG 格式!');
        console.log('fileList', this.fileList)
        return Promise.reject(); // 返回一个被拒绝的Promise
      }
      return true; // 所有条件通过则允许上传
    },
    successHandle (res, file, fileList) {
      this.dataForm.iconUrl = res.data.src
    },
    init () {
      this.visible = true
      this.fileList = []
      this.$nextTick(() => {
        this.$refs.dataForm.resetFields()
        if (this.dataForm.id) {
          this.getInfo()
        }
      })
    },
    // 获取信息
    getInfo () {
      this.$http
        .get(`cgjybzIndustryCloudMapInfo/${this.dataForm.id}`)
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.dataForm = {
            ...this.dataForm,
            ...res.data
          }
        })
        .catch(() => {})
    },
    // 表单提交
    dataFormSubmitHandle: debounce(
      function () {
        this.$refs.dataForm.validate((valid) => {
          if (!valid) {
            return false
          }
          this.$http
            .post('/cgjybzIndustryCloudMapInfo/save', this.dataForm)
            .then(({ data: res }) => {
              if (res.code !== 0) {
                return this.$message.error(res.msg)
              }
              this.$message({
                message: this.$t('prompt.success'),
                type: 'success',
                duration: 500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            })
            .catch(() => {})
        })
      },
      1000,
      { leading: true, trailing: false }
    )
  }
}
</script>
